import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @description: 标签切换
 * @author: L.Wen
 * @create: 2018-10-13 09:53
 **/
public class TestTags {

    private static WebDriver driver;
    private WebElement webElement = null;
    private WebElement webElement1 = null;

    static {
        //告诉系统chromedriver.exe的位置
        System.setProperty("webdriver.chrome.driver", "C:\\Programs\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test()
    public void openBD() {
        System.out.println("Test openBD! \n");
//        driver.get("http://www.baidu.com");
        driver.get("https://youliao.163yun.com/h5/list/?appkey=7a5c4732c2084c54bba26859ad5e9541&secretkey=9630363599734af6872167c54a461ee8&s=semi");

    }

    @Test(priority = 10)
    public void searchCSDN() throws InterruptedException {
        for (int i = 0; i < 1; i++) {

            System.out.println("Test search头条! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_1\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search娱乐! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_2\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search体育! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_3\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search搞笑! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_4\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search生活! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_5\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search搞笑! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_4\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search体育! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_3\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search娱乐! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_2\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search头条! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_1\"]"));
            webElement.click();
            Thread.sleep(2001);

            System.out.println("Test search1Call! \n");
            webElement = driver.findElement(By.xpath("//*[@data-key = \"t_0\"]"));
            webElement.click();
            Thread.sleep(2001);
        }
    }

    @Test(priority = 20)
    public void clickResult() throws InterruptedException {
        System.out.println("Test clickResult! \n");
        webElement = driver.findElement(By.xpath("//*[@target=\"_blank\"]"));
        webElement.click();
        Thread.sleep(2001);
    }

    @Test(priority = 30)
    public void clickAdver() throws InterruptedException {
//        driver.switchTo().frame("");
        Set<String> windows = driver.getWindowHandles();
        List<String> windowsArr = new ArrayList<>(windows);
        driver.switchTo().window(windowsArr.get(1));
//        webElement = driver.findElement(ByAll.xpath("//*[@data-id=\"youliao-ad-top-0\"]"));
        webElement = driver.findElement(By.xpath("//*[@data-id=\"youliao-ad-top-0\"]"));
//        webElement1 = driver.findElement(By.xpath("/html/body/div[0]/div[0]/div[1]/div[0]/div[1]/iframe[0]"));
        webElement1 = driver.findElement(By.tagName("iframe"));
        webElement.click();
        System.out.println(webElement1.getAttribute("src"));
        Thread.sleep(2001);
    }

    @Test(priority = 40)
    public void searchResult() {
        System.out.println("Test searchResult! \n");
        String pageSourcerce = driver.getPageSource();
        System.out.println("pageSourcerce: \n" + pageSourcerce + "\n");
        String pageSourcerce1 = driver.findElement(By.tagName("body")).getText();
        System.out.println("pageSourcerce1: \n" + pageSourcerce1 + "\n");
//        Assert.assertTrue(pageSourcerce.contains("CSDN-专业IT技术社区"));
//        webElement = driver.findElement(By.xpath("//*[@class=\"favurl\"]"));
//        webElement.click();
    }
}
